<?php
declare (strict_types=1);

namespace app\exam\logic;

use app\common\logic\category\Category;
use think\facade\Db;
use think\facade\Cache;
use think\facade\Request;
use app\common\exception\LogicException;
use app\common\model\ProductType;

use app\exam\model\Rollup as RollupModel;

/**
 * 套卷练习逻辑
 */
class Rollup extends RollupModel
{

    /**
     * 获取套卷练习列表
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-03-30
     *
     * @param  [type] $param [description]
     * @return [type]        [description]
     */
    public function getRollupList($param)
    {
        // 验证数据
        $this->getValidate()->scene('rollup_list')->failException()->check($param);
        // 参数
        $moduleId = $param['module_id'];
        $category = $param['category']??0;
        if(!$category) {
            /** @var Category $cateLogic */
            $cateLogic = app(Category::class);
            $category = $cateLogic->getFirstTopId(3);
        }
        // 获取套卷列表
        $list = $this
            ->whereModuleId($moduleId)
            ->whereLike('category', '%,' . $category . ',%')
            ->withCount([
                'rollup_paper' => 'paper_count',
                'has_bought'   => 'has_bought',
                'bought_num'   => 'bought_count',
                'practice_num' => 'practice_count'
            ])
            ->with(['first_paper_id'])
            ->paginate($this->getListRows());

        return $list;
    }

    /**
     * 搜索套卷列表
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-02-21
     *
     * @param  [type] $param [description]
     * @return [type]        [description]
     */
    public function search($param)
    {
        // 验证数据
        $this->getValidate()->scene('rollup_search')->failException()->check($param);
        // 参数
        $moduleId = $param['module_id'];
        $category = $param['category'];
        $title    = $param['title'];
        // 获取套卷列表
        $list = $this
            ->whereModuleId($moduleId)
            ->whereLike('category', '%,' . $category . ',%')
            ->whereLike('title', '%' . $title . '%')
            ->withCount([
                'rollup_paper' => 'paper_count',
                'has_bought'   => 'has_bought',
                'bought_num'   => 'bought_count',
                'practice_num' => 'practice_count'
            ])
            ->paginate($this->getListRows());

        return $list;
    }

    /**
     * 套卷详情
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-03-31
     *
     * @param array $param [description]
     * @return [type]        [description]
     */
    public function getRollupDetail(array $param)
    {
        // 数据检测
        $this->getValidate()->scene('rollup_detail')->failException()->check($param);
        // 参数
        $rollup_id = $param['rollup_id'];
        // 数据
        $data = $this
            ->with(['rollup_paper' => function ($query) use ($rollup_id) {
                $query->with(['paper' => function ($query) use ($rollup_id) {
                    $query->field('id,title')
                        ->withCount([
                            'bought_num'   => 'bought_num',
                            'practice_num' => function ($query) use ($rollup_id) {
                                $query->where('rollup_id', $rollup_id);
                            }
                        ])
                        ->bind(['title', 'bought_num', 'practice_count' => 'practice_num_count']);
                }]);
            }])
            ->withCount([
                'has_bought'   => 'has_bought',
                'rollup_paper' => 'paper_count',
                'practice_num' => 'practice_count'
            ])
            ->findOrEmpty($rollup_id);

        return $data;
    }

    /**
     * 获取套卷名称
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-01
     *
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function getTitleById($id)
    {
        return $this->whereId($id)->value('title');
    }

    /**
     * 订单产品详情
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-04-02
     *
     * @param  [type] $orderItem [description]
     * @return [type]            [description]
     */
    public function getProductInfo($orderItem)
    {
        $info              = $this->field('id,title,price,module_id')->with(['mhm_id'])->findOrEmpty($orderItem->product_id)->hidden(['module_id']);
        $info->type_id     = ProductType::getProductIdByKey('rollup');
        $info->payment     = $orderItem->payment;
        $info->expire_time = $orderItem->expire_time;
        $info->expire_rest = $orderItem->expire_time ? ceil(max(0, $orderItem->expire_time - time()) / 86400) : -1;

        return $info;
    }

}